package com.myproject.cas;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class UnsafeTest {
    private static Unsafe unsafe;

    static {
        try {
            //通过反射获取rt.jar下的Unsafe类
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (Unsafe) field.get(null);
        } catch (Exception e) {
            System.out.println("Get Unsafe instance occur error"+ e);
        }
    }




    public static void main(String[] args) throws Exception
    {
        Class clazz = Target.class;
        Field[] fields = clazz.getDeclaredFields();
        System.out.println("fieldName:fieldOffset");
        for (Field f : fields) {
            // 获取属性偏移量，可以通过这个偏移量给属性设置
            System.out.println(f.getName() + ":" + unsafe.objectFieldOffset(f));
        }
        Target target = new Target();
        System.out.println(target);
        Field intFiled  =  clazz.getDeclaredField("intParam")  ;
        int a=(Integer)intFiled.get(target ) ;
        System.out.println("原始值是:"+a);
        //intParam的字段偏移是12 原始值是3 我们要改为10
        System.out.println(unsafe.compareAndSwapInt(target, 12, 3, 10));
        int b=(Integer)intFiled.get(target) ;
        System.out.println("改变之后的值是:"+b);

        //这个时候已经改为10了,所以会返回false
        System.out.println(unsafe.compareAndSwapInt(target, 12, 3, 10));

        System.out.println(unsafe.compareAndSwapObject(target, 24, null, "5"));
        System.out.println(target);

    }

}

class Target {
    Baby baby1;
    Baby baby2;
    Baby baby3;
    boolean b1;
    boolean b2;
    boolean b3;
    char c1;
    char c2;
    char c3;
    int intParam=3;
    long longParam;
    String strParam="我和我的祖国一刻也不能分割";
    String strParam2;

    @Override
    public String toString() {
        return "Target{" +
                "intParam=" + intParam +
                ", longParam=" + longParam +
                ", strParam='" + strParam + '\'' +
                ", strParam2='" + strParam2 + '\'' +
                '}';
    }
}